﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using CMN.Models;
using CMN.Models.Interfaces;
using CMN.Entity;
using System.Web.Routing;
using CMN.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using CMN.Controllers.Filters;
namespace CMN.Controllers
{
    [ExceptionFilter]
    [LoggerFilter]
    [UserAuthorize]
    public class Normal_TemplateTypeController : Controller
    {
        
        // 返回json字符串
        protected string returnJson = string.Empty;

        /// <summary>
        /// 转到View页面
        /// </summary>
        /// <returns></returns>
        public ViewResult Index()
        {
            return View("Index");//页面地址
        }

        /// <summary>
        /// 返回模板类型列表
        /// </summary>
        /// <returns>json</returns>
        public string List(string actionId)
        {
            //返回json数组
            if (actionId == "m")
            {
              
                QueryParam qp = new QueryParam();
                qp.PageIndex = 1;
                qp.PageSize = 100;
                int recordNum = 0;
              
                IDataService<Normal_TemplateTypeEntity> Normal_TemplateTypeSv = ServiceBuilder.BuildNormal_TemplateTypeService();
                List<Normal_TemplateTypeEntity> normal_templatetypeList = Normal_TemplateTypeSv.List(qp, out recordNum);
                returnJson = string.Concat("{totalPorperty:", recordNum, ",root:", JsonConvert.SerializeObject(normal_templatetypeList), "}");
            }
            else
            {
                int pageSize = (int)Utils.GetQueryAndFormString("limit", this, 4, 1, DataType.Int);
                int pageIndex = (int)Utils.GetQueryAndFormString("start", this, 9, 1, DataType.Int);
                QueryParam qp = new QueryParam();
                qp.PageIndex = pageIndex / pageSize + 1; ;
                qp.PageSize = pageSize;
                int recordNum = 0;
                IsoDateTimeConverter jsDataConvert = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
                jsDataConvert.DateTimeFormat = "yyyy-MM-dd";
                IDataService<Normal_TemplateTypeEntity> Normal_TemplateTypeSv = ServiceBuilder.BuildNormal_TemplateTypeService();
                List<Normal_TemplateTypeEntity> normal_templatetypeList = Normal_TemplateTypeSv.List(qp, out recordNum);
                returnJson = string.Concat("{totalPorperty:", recordNum, ",root:", JsonConvert.SerializeObject(normal_templatetypeList, Formatting.Indented, jsDataConvert), "}");
           
            }
            return returnJson;
 
        }

        /// <summary>
        /// 查看模板类型 
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Look(string id)
        {
           IDataService<Normal_TemplateTypeEntity> Normal_TemplateTypeSv = ServiceBuilder.BuildNormal_TemplateTypeService();
            int intId=Utils.StringToInt(id,0);
            Normal_TemplateTypeEntity normal_templatetype = Normal_TemplateTypeSv.Look(intId);
            if (normal_templatetype != null)
                returnJson = string.Concat("{success:true,data:", JsonConvert.SerializeObject(normal_templatetype), "}");
            else
                returnJson = "{success:'false'}";
            return returnJson;
        }

        /// <summary>
        /// 更新模板类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Update(string id)
        {
            int intId = Utils.StringToInt(id, 0);
            if (intId != 0)
            {
                Normal_TemplateTypeEntity normal_templatetype = new Normal_TemplateTypeEntity();
                normal_templatetype.DataTable_Action = DataTableAction.Update;
                normal_templatetype.Id = intId;
                normal_templatetype.TemplateType = (String)Utils.GetQueryAndFormString("TemplateType", this, 30,  1 , DataType.Str);
                normal_templatetype.TemplateTypeName = (String)Utils.GetQueryAndFormString("TemplateTypeName", this, 50,  1 , DataType.Str);
                IDataService<Normal_TemplateTypeEntity> Normal_TemplateTypeSv = ServiceBuilder.BuildNormal_TemplateTypeService();
                if (Normal_TemplateTypeSv.Update(normal_templatetype) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";
            }
            else
            {
                returnJson = "{success:'false'}";
            }
           
            return returnJson;
        }

        /// <summary>
        /// 新增模板类型
        /// </summary>
        /// <returns>json</returns>
        public string Insert()
        {
            Normal_TemplateTypeEntity normal_templatetype = new Normal_TemplateTypeEntity();
            normal_templatetype.DataTable_Action = DataTableAction.Insert;
            normal_templatetype.TemplateType = (String)Utils.GetQueryAndFormString("TemplateType", this, 30,  1 , DataType.Str);
            normal_templatetype.TemplateTypeName = (String)Utils.GetQueryAndFormString("TemplateTypeName", this, 50,  1 , DataType.Str);
           IDataService<Normal_TemplateTypeEntity> Normal_TemplateTypeSv = ServiceBuilder.BuildNormal_TemplateTypeService();
           if (Normal_TemplateTypeSv.Insert(normal_templatetype) == "")
            {
                returnJson = "{success:'true'}";
            }
            else
                returnJson = "{success:'false'}";
            return returnJson;
        }

        /// <summary>
        /// 删除模板类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Delete(string id)
        {
            int intId = Utils.StringToInt(id, 0);
            if (intId != 0)
            {
                Normal_TemplateTypeEntity normal_templatetype = new Normal_TemplateTypeEntity();
                normal_templatetype.Id = intId;
                IDataService<Normal_TemplateTypeEntity> Normal_TemplateTypeSv = ServiceBuilder.BuildNormal_TemplateTypeService();
                if (Normal_TemplateTypeSv.Delete(normal_templatetype) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";
            }
            else
            {
                returnJson = "{success:'false'}";
            }
            return returnJson;
        }

        /// <summary>
        /// 批量删除模板类型
        /// </summary>
        /// <param name="idList">id列表 234_4_5</param>
        /// <returns>json</returns>
        public string DeleteList(string id)
        {
            Common.RegExpression.NumAndBarRegExpression regex = new CMN.Common.RegExpression.NumAndBarRegExpression();
            if (regex.IsMatch(id))
            {

                IDataService<Normal_TemplateTypeEntity> Normal_TemplateTypeSv = ServiceBuilder.BuildNormal_TemplateTypeService();
                if (Normal_TemplateTypeSv.DeleteList(id) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";

            }
            else
            {
                returnJson = "{success:'false',message:'参数类型不正确'}";
            }
            return returnJson;
        }

    }
}
